package com.its.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.github.pagehelper.PageInfo;
import com.its.pojo.BusClaimReport;
import com.its.pojo.BusDispatchInfo;
import com.its.pojo.HupCompany;
import com.its.pojo.WorkingPersonnel;
import com.its.service.BusClaimReportService;
import com.its.service.BusDispatchInfoService;
import com.its.service.HupCompanyService;
import com.its.service.WorkingPersonnelService;
import com.its.util.HttpStatusEnum;
import com.its.util.Result;
import com.its.web.param.BusDispatchInfoControllerParam;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 调度信息表 前端控制器
 * </p>
 *
 * @author 朱厚强
 * @since 2023年12月27日
 */
@RestController
@RequestMapping("/busDispatchInfo")
@Api(tags = "调度信息-api")
public class BusDispatchInfoController {
    @Autowired
    private BusDispatchInfoService service;
    @Autowired
    private BusClaimReportService busClaimReportService;
    @Autowired
    private WorkingPersonnelService workingPersonnelService;
    @Autowired
    private HupCompanyService hupCompanyService;
    /**
     * 获取所有的调度信息
     * @return
     */
    @PostMapping("/pageList")
    public Result pagingList(@RequestBody BusDispatchInfoControllerParam param){
        PageInfo<BusDispatchInfo> pageInfo = service.selectList(param);
        if (pageInfo.getSize() > 0){  // isEmpty()方法在列表为空时返回true，否则返回false。 Empty(中译 空的)
            return Result.ok().data(pageInfo).code(HttpStatusEnum.SUCCESS.getCode()).message("返回调度信息");
        }else {
            return Result.error().data(pageInfo).code(HttpStatusEnum.ERROR.getCode()).message("返回调度信息");
        }
    }
    /**
     * 删除报案信息(逻辑删除)
     * @return
     */
    @PostMapping("/remove")
    public Result remove(@RequestBody BusDispatchInfo busDispatchInfo){
        boolean b = service.delete(busDispatchInfo);
        if (b){
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).message("撤回调度信息成功");
        }else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).message("撤回调度信息失败");
        }
    }
    /**
     * 获取指派中任务
     * @return
     */
    @GetMapping("/queryTask")
    public Result queryTask(@RequestParam Integer dispatchId){
        dispatchId = 2; // 目前调度员信息写死，后续再做更改。
        List<BusClaimReport> claimReportList = service.queryTask(dispatchId);
        if (claimReportList.size()>0){
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).data(claimReportList).message("查询用户要处理的报案信息成功");
        }else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).data(claimReportList).message("查询用户要处理的报案信息失败");
        }
    }
    /**
     * 任务拉取(逻辑删除)
     * @return
     */
    @PutMapping("/selectTask")
    public Result selectTask(@RequestBody BusClaimReport claimReport){
        boolean b = service.selectTaskS(claimReport);
        if (b){
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).message("拉取任务成功");
        }else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).message("拉取任务失败");
        }
    }
    /**
     * 撤销指派任务
     * @return
     */
    @PutMapping("/backoutTask/{claimReportId}")
    public Result selectTask(@PathVariable("claimReportId") String claimReportId) {
        UpdateWrapper<BusClaimReport> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("flow_state", 0)
                .set("dispatch_id", null)
                .eq("claim_report_id", claimReportId);
        boolean b = busClaimReportService.update(updateWrapper);
        if (b) {
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).message("撤销指派任务成功");
        } else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).message("撤销指派任务失败");
        }
    }
    /**
     * 根据企业id查询调度员信息
     * @return
     */
    @GetMapping("/selectCompanyById")
    public Result selectCompanyById (@RequestParam String companyId){
        List<WorkingPersonnel> list = workingPersonnelService.selectCompanyById(companyId);
        if (list !=null){
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).data(list).message("根据企业id查询调度员信息成功");
        }else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).data(list).message("根据企业id查询调度员信息失败");
        }
    }
    /**
     * 增加指派（调度）信息
     * @return
     */
    @PostMapping("/addDispatch")
    public Result addDispatch (@RequestBody BusDispatchInfo dispatchInfo){
        Boolean b = service.addDispatch(dispatchInfo);
        if (b){
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).message("指派任务成功");
        }else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).message("指派任务失败");
        }
    }

}
